//
//  ExchangeTwoViewViewController.m
//  LineMoney
//
//  Created by 范琦 on 16/7/14.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "ExchangeTwoViewViewController.h"
#import "ExchangeTwoTopView.h"
#import "ExchangeTwoCell.h"
#import "MainSectionView.h"
#import "ExchangeThreeViewController.h"
#import "ExchangeAddViewController.h"

@interface ExchangeTwoViewViewController ()<UITableViewDataSource, UITableViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIView *mainView;

@property (nonatomic, strong) ExchangeTwoTopView *topView;
@property (nonatomic, strong) MainSectionView *sectionView;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) NSMutableArray *textArr;

@property (nonatomic, strong) UIPickerView *pickView;
@property (nonatomic, strong) UIView *pickTopView;

@property (nonatomic, strong) NSMutableArray *yearArr;
@property (nonatomic, strong) NSMutableArray *mounthArr;
@property (nonatomic, strong) NSMutableArray *dayArr;

@property (nonatomic, strong) NSString *str;
@property (nonatomic, strong) NSString *datStr;



@end

@implementation ExchangeTwoViewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = HUIBACKGROUNDCOLOR;
    self.title = @"汇款账户";
    self.mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT)];
    [self.view addSubview:_mainView];
    _str = @"范琦";
    self.tabBarController.tabBar.hidden = YES;
    [self creatTopView];
    
    [self creatTableView];
    [self creatSectionView];
    [self creatBottomButton];
    [self creatRightBtn];
}
- (void)creatRightBtn
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0, 0, 30, 30);
    //    button.backgroundColor = [UIColor redColor];
    UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithCustomView:button];
    [button setImage:[UIImage imageNamed:@"添加-0"] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = btn;
    [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        ExchangeAddViewController *add = [[ExchangeAddViewController alloc]init];
        add.str1 = @"汇款";
        [self.navigationController pushViewController:add animated:YES];
    }];
    
    
}
- (void)creatTopView
{
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"ExchangeTwoTopView" owner:nil options:nil] lastObject];
    _topView.frame = CGRectMake(0, 0, KSCREENWIDTH, 75);
    [self.mainView addSubview:_topView];
    _topView.ImageV.image = [UIImage imageNamed:@"进程22"];

}

- (void)creatSectionView
{
    self.sectionView = [[[NSBundle mainBundle] loadNibNamed:@"MainSectionView" owner:nil options:nil] lastObject];
    [_mainView addSubview:_sectionView];
    _sectionView.frame = CGRectMake(0, 85, KSCREENWIDTH, 40 );
    _sectionView.backgroundColor = [UIColor whiteColor];
    _sectionView.leftView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"小标题前竖条"]];
    _sectionView.label.textColor = BACKGROUNDCOLOR;
    _sectionView.backgroundColor = [UIColor whiteColor];
    _sectionView.label.text = @"现有记录";
    [[_sectionView.btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
       
        [self creatPickerView:_sectionView.btn];
        
    }];

}

- (void)creatTableView
{
    self.arr = [NSMutableArray arrayWithObjects:@"账户名",@"账户所有者",@"银行账户",@"再输入账户",@"账户密码",@"再输入密码", nil];
    
    self.textArr = [NSMutableArray arrayWithObjects:@"账号户名",@"王姜博",@"2106 4565 1225 8411 321",@"2106 4565 1225 8411 321",@"",@"", nil];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 135, KSCREENWIDTH, 55 * 6)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.mainView addSubview:_tableView];
    _tableView.scrollEnabled = NO;
    _tableView.rowHeight = 55;
    [_tableView registerNib:[UINib nibWithNibName:@"ExchangeTwoCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (void)creatBottomButton
{
    UIButton *nextBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    nextBtn.backgroundColor = BACKGROUNDCOLOR;
    [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
    [_mainView addSubview:nextBtn];
    nextBtn.frame = CGRectMake(0, KSCREENHEIGHT - 64 - 40 , KSCREENWIDTH, 40);
    nextBtn.tintColor = [UIColor whiteColor];
    [[nextBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        ExchangeThreeViewController *C = [[ExchangeThreeViewController alloc]init];
        [self.navigationController pushViewController:C animated:YES];
    }];


}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 6;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExchangeTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.label.text = _arr[indexPath.row];

    cell.text.text = _textArr[indexPath.row];
    cell.imageV.hidden = YES;
    if (indexPath.row == 4 || indexPath.row == 5) {
        cell.text.secureTextEntry = YES;
    }
    return cell;
}

#warning 创建PickerView
- (void)creatPickerView:(UIButton *)btn
{
    if (_pickView == nil) {
        self.yearArr = [NSMutableArray arrayWithObjects:@"范琦",@"王姜博",@"尹宁", nil];
        self.dayArr = [NSMutableArray arrayWithObjects:@"2986 4565 1125 8641 532",@"2106 4565 1225 8411 321",@"2432 4545 12445 8891 123", nil];
        self.datStr = @"2106 4565 1225 8411 321";
        self.pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, KSCREENHEIGHT - 150, KSCREENWIDTH, 150)];
        _pickView.frame = CGRectMake(0, KSCREENHEIGHT - 144, KSCREENWIDTH, 144);
        _pickView.delegate = self;
        _pickView.dataSource = self;
        [self.view addSubview:_pickView];
        _pickView.backgroundColor = [UIColor huiseColor];
        
        self.pickTopView = [[UIView alloc]initWithFrame:CGRectMake(0, _pickView.frame.origin.y - 30, KSCREENWIDTH, 30)];
        [self.view addSubview:_pickTopView];
        _pickTopView.backgroundColor = HUIBACKGROUNDCOLOR;
        
    }else{
        [self.view addSubview:_pickView];
        [self.view addSubview:_pickTopView];
    }
    UIButton *done = [UIButton buttonWithType:UIButtonTypeSystem];
    [done setTitle:@"Done" forState:UIControlStateNormal];
    done.frame = CGRectMake(KSCREENWIDTH - 60, 0, 40, 30);
    done.tintColor = [UIColor blackColor];
    [_pickTopView addSubview:done];
    [[done rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        [_pickView removeFromSuperview];
        [_pickTopView removeFromSuperview];

        
        ExchangeTwoCell *cell = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
        cell.text.text = _str;
        ExchangeTwoCell *cell1 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0]];
        cell1.text.text = _datStr;
        ExchangeTwoCell *cell2 = [_tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:3 inSection:0]];
        cell2.text.text = _datStr;
    }];
    
}
#warning PickerView代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    
    return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//    if (component == 0) {
//        return _yearArr[row];
//    }else if (component == 1){
//        return _mounthArr[row];
//    }else{
//    
//        return _dayArr[row];
//    }
    return _yearArr[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _str = _yearArr[row];
    _datStr = _dayArr[row];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
